package lerry.utils;
import flash.display.DisplayObjectContainer;
import flash.events.MouseEvent;
import flash.Lib;
import lerry.display.StageInfo;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
#if android
import flash.text.Font;
#end
/**
 * ...
 * @author berry
 */
class Debug
{
	static var stage:DisplayObjectContainer;
	static var info:TextField;
	static var fps:FPS;
	
	static var format:TextFormat;
	
	static var isd:Bool;
	static var dy:Float;
	static var temp:String;
	
	public static var color(get_color,set_color):UInt;
	
	static private function get_color():UInt{
		return format != null?format.color:0;
	}
	
	static private function set_color(val:UInt):UInt {
		if (format != null) {
			format.color = val;
		}
		return val;
	}
	
	/**
	 * 在屏幕上打印信息
	 * @param	obj
	 */
	static public function log(obj:Dynamic):Void {
		if (info == null) {
			info = new TextField();
			StageInfo.STAGE.addChild(info);
			info.selectable = false;
			info.wordWrap = true;
			//info.mouseEnabled = false;
			info.height = StageInfo.STAGE_HEIGHT;
			
			#if android
			format = new TextFormat(new Font("/system/fonts/MTLmr3m.ttf").fontName);
			#else
			format = new TextFormat();
			format.font = "微软雅黑";
			#end
			format.size = 12;
			format.color = color;
			
			info.defaultTextFormat = format;
			info.addEventListener(MouseEvent.MOUSE_DOWN, function(e):Void { 
				isd = true;
				dy = StageInfo.STAGE.mouseY;
			} );
			info.addEventListener(MouseEvent.MOUSE_UP, function(e):Void { 
				isd = false;
				dy = StageInfo.STAGE.mouseY;
			} );
			#if android
			#else
			info.addEventListener(MouseEvent.MOUSE_WHEEL, function(e):Void { 
				info.scrollV += -e.delta;
			} );
			#end
			info.addEventListener(MouseEvent.MOUSE_MOVE, function(e):Void { 
				if (isd) {
					var dis = StageInfo.STAGE.mouseY - dy;
					info.scrollV = Std.int(dis);
				}
				
			} );
			log("初始化成功");
			temp = "";
		}
		info.appendText(">-"+Std.string(obj) + "\n");
	}
	/**
	 * 显示fps
	 */
	static public function showFPS():Void {
		if (fps == null) {
			fps = new FPS();
			info.y = fps.height;
		}
		Lib.current.stage.addChild(fps);
		
	}
}